package com.yf.zzmusic.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yf.zzmusic.common.Constants;
import com.yf.zzmusic.common.PageInfo;
import com.yf.zzmusic.common.Result;
import com.yf.zzmusic.pojo.dto.album.AlbumPageDTO;
import com.yf.zzmusic.pojo.dto.album.AlbumUpdateDTO;
import com.yf.zzmusic.pojo.dto.music.MusicPageDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

import com.yf.zzmusic.service.IAlbumService;
import com.yf.zzmusic.pojo.entity.Album;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 专辑表 前端控制器
 * </p>
 *
 * @author
 * @since 2024-03-07
 */
@RestController
@RequestMapping("/album")
public class AlbumController {
    @Resource
    @Autowired
    private IAlbumService albumService;

    @PostMapping("/insert")
    public Result insert(@Validated @RequestBody Album album) {
        boolean result=albumService.save(album);
        if (result){
            return Result.success("新增成功",null);
        }else{
            return Result.error("新增失败");
        }
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        boolean result = albumService.removeById(id);
        if (result){
            return Result.success("删除成功",null);
        }else{
            return Result.error("删除失败");
        }
    }

    @PostMapping("/update")
    public Result insert(@Validated @RequestBody AlbumUpdateDTO albumUpdateDTO){
        boolean result=albumService.updateAlbum(albumUpdateDTO);
        if (result){
            return Result.success("修改成功",null);
        }else{
            return Result.error("删除失败");
        }
    }

    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        Album album=albumService.getById(id);
        if (album!=null){
            return Result.success("查询成功",album);
        }else{
            return Result.error("查询失败");
        }
    }

    @GetMapping("/page")
    public Result findPage(@Validated AlbumPageDTO albumPageDTO) {
        PageInfo<Album> pageInfo=albumService.albumByPage(albumPageDTO);
        if (pageInfo.getTotal()>0){
            return Result.success("查询成功",pageInfo);
        }else{
            return Result.error(Constants.NO_CONTENT,"列表为空");
        }
    }

}

